<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Admin_Form_User{
	
    protected $_isError = false;
	protected $_messageError = null;
    protected $_arrData = null;
    protected $_classError = null;
    
    public function __construct($params = array(), $data = null){
        $translate = Zend_Registry::get('Zend_Translate');
        
        $validateName = new Zend_Validate();
        $validateAddress = new Zend_Validate();
        $validatePhoneNumber = new Zend_Validate();
        $validateEmail = new Zend_Validate();
        $validateUsername = new Zend_Validate();
        $validatePassword = new Zend_Validate();
        $validateRePassword = new Zend_Validate();
        
        $validateName->addValidator(new Zend_Validate_NotEmpty, true)
                        ->addValidator(new Zend_Validate_StringLength(array('max' => 50)), true)
                        ->addValidator(new Zend_Validate_Alnum(true), true);
        
        $validateName->addValidator(new Zend_Validate_StringLength(array('max' => 255)), true);
        
        $validatePhoneNumber->addValidator(new Zend_Validate_Digits(), true);
        
        $emailOption = array('table' => 'users', 'field' => 'email');
        
        if($data != null){
            $emailOption['exclude'] = array ('field' => 'id', 'value' => $data['id']);
        }
         
        $validateEmail->addValidator(new Zend_Validate_NotEmpty(), true)
                        ->addValidator(new Zend_Validate_EmailAddress(), true)
                        ->addValidator(new Zend_Validate_StringLength(array('max' => 100)), true)
                        ->addValidator(new Zend_Validate_Db_NoRecordExists($emailOption), true);
        
        $usernameOption = array('table' => 'users', 'field' => 'username');
        
        if($data != null){
            $usernameOption['exclude'] = array ('field' => 'id', 'value' => $data['id']);
        }
         
        $validateUsername->addValidator(new Zend_Validate_NotEmpty, true)
                        ->addValidator(new Zend_Validate_StringLength(array('max' => 30)), true)
                        ->addValidator(new Zend_Validate_Alnum(true), true)
                        ->addValidator(new Zend_Validate_Db_NoRecordExists($usernameOption), true);
        
        $validatePassword->addValidator(new Zend_Validate_NotEmpty(), true)
                        ->addValidator(new Zend_Validate_StringLength(array('min' => 6, 'max' => 100)), true);
        
        $validateRePassword->addValidator(new Zend_Validate_NotEmpty(), true)
                        ->addValidator(new Zend_Validate_StringLength(array('min' => 6, 'max' => 100)), true)
                        ->addValidator(new Zend_Validate_Identical($params['password']), true);
        
        /*
        * Check name
        */
        
        if(!$validateName->isValid($params['name'])){
            $this->_messageError['name'] = '<label class="control-label" for="inputError">' . $translate->_('name') . current($validateName->getMessages()) . '</label>';
            $this->_arrData['name'] = '';
            $this->_classError['name'] = 'has-error';
            $this->_isError = true;
            
            if($data != null){
                $this->_arrData['name'] = $data['name'];
            }
        }else{
            $this->_messageError['name'] = '';
            $this->_arrData['name'] = $params['name'];
            $this->_classError['name'] = '';
        }
        
        /*
        * Check address
        */
        
        if(!$validateAddress->isValid($params['address'])){
            $this->_messageError['address'] = '<label class="control-label" for="inputError">' . $translate->_('address') . current($validateAddress->getMessages()) . '</label>';
            $this->_arrData['address'] = '';
            $this->_classError['address'] = 'has-error';
            $this->_isError = true;
            
            if($data != null){
                $this->_arrData['address'] = $data['address'];
            }
        }else{
            $this->_messageError['address'] = '';
            $this->_arrData['address'] = $params['address'];
            $this->_classError['address'] = '';
        }
        
        /*
        * Check phone_number
        */
        
        if(!$validatePhoneNumber->isValid($params['phone_number'])){
            $this->_messageError['phone_number'] = '<label class="control-label" for="inputError">' . $translate->_('phone number') . current($validatePhoneNumber->getMessages()) . '</label>';
            $this->_arrData['phone_number'] = '';
            $this->_classError['phone_number'] = 'has-error';
            $this->_isError = true;
            
            if($data != null){
                $this->_arrData['phone_number'] = $data['phone_number'];
            }
        }else{
            $this->_messageError['phone_number'] = '';
            $this->_arrData['phone_number'] = $params['phone_number'];
            $this->_classError['phone_number'] = '';
        }
        
        /*
        * Check email
        */
        
        if(!$validateEmail->isValid($params['email'])){
            $this->_messageError['email'] = '<label class="control-label" for="inputError">' . $translate->_('email') . current($validateEmail->getMessages()) . '</label>';
            $this->_arrData['email'] = '';
            $this->_classError['email'] = 'has-error';
            $this->_isError = true;
            
            if($data != null){
                $this->_arrData['email'] = $data['email'];
            }
        }else{
            $this->_messageError['email'] = '';
            $this->_arrData['email'] = $params['email'];
            $this->_classError['email'] = '';
        }
        
        /*
        * Check Description
        */
        
        if(!$validateUsername->isValid($params['username'])){
            $this->_messageError['username'] = '<label class="control-label" for="inputError">' . $translate->_('username') . current($validateUsername->getMessages()) . '</label>';
            $this->_arrData['username'] = '';
            $this->_classError['username'] = 'has-error';
            $this->_isError = true;
            
            if($data != null){
                $this->_arrData['username'] = $data['username'];
            }
        }else{
            $this->_messageError['username'] = '';
            $this->_arrData['username'] = $params['username'];
            $this->_classError['username'] = '';
        }
        
        if($data == null || ($data != null && $params['password'] != null) || ($data != null && $params['repassword'] != null)){
            /*
            * Check password
            */
            
            if(!$validatePassword->isValid($params['password'])){
                $this->_messageError['password'] = '<label class="control-label" for="inputError">' . $translate->_('password') . current($validatePassword->getMessages()) . '</label>';
                $this->_arrData['password'] = '';
                $this->_classError['password'] = 'has-error';
                $this->_isError = true;
            }else{
                $this->_messageError['password'] = '';
                $this->_arrData['password'] = '';
                $this->_classError['password'] = '';
            }
            
            /*
            * Check repassword
            */
            
            if(!$validateRePassword->isValid($params['repassword'])){
                $this->_messageError['repassword'] = '<label class="control-label" for="inputError">' . $translate->_('repassword') . current($validateRePassword->getMessages()) . '</label>';
                $this->_arrData['repassword'] = '';
                $this->_classError['repassword'] = 'has-error';
                $this->_isError = true;
            }else{
                $this->_messageError['repassword'] = '';
                $this->_arrData['repassword'] = $params['repassword'];
                $this->_classError['repassword'] = '';
            }
        }
        
        $status = true;
        
        if(!isset($params['status'])){
            $status = false;
        }
        
        $role = true;
        
        if(!isset($params['role'])){
            $role = false;
        }
        
        $block = true;
        
        if(!isset($params['block'])){
            $block = false;
        }
        
        $this->_arrData['status'] = $status;
        $this->_arrData['role'] = $role;
        $this->_arrData['block'] = $block;
        
        if($data != null){
            $this->_arrData['id'] = $data['id'];
        }
        
        $date = new Zend_Date();
        $curDate = $date->get(Zend_Registry::get('date_format_insert'));
        
        if($data == null){
            $this->_arrData['register_date'] = $curDate;
        }
        
        if(!$this->_isError){
            $this->_arrData['password'] = sha1($params['password']);
            unset($this->_arrData['repassword']);
        }
    }
    
    public function isError(){
    	return $this->_isError;
    }
    
    public function getMessageError(){
        return $this->_messageError;
    }
    
    public function getClassError(){
    	return $this->_classError;
    }
    
    public function getData(){
        return $this->_arrData;
    }
}